package cc.yihy.storage.ftp;

import cc.yihy.storage.StorageClient;
import cc.yihy.storage.ftp.bean.FtpClient;
import cc.yihy.storage.ftp.bean.SimpleFtpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Yihy
 * @version V1.0
 * @Package com.peoplecitic.news.management.config
 * @Description:
 * @date 2017/7/21 10:41
 */
@Configuration
@ConditionalOnClass({FtpClient.class, SimpleFtpClient.class})
@EnableConfigurationProperties(value = FtpProperties.class)
public class FtpAutoConfiguration {

    @Autowired
    private FtpProperties properties;


    @Bean
    @ConditionalOnMissingBean
    public FtpClient ftpClient() {
        FtpClient ftpClient = new FtpClient(properties.getHost(),
                properties.getPort(),
                properties.getUsername(),
                properties.getPassword(),
                properties.getRemoteDicrory());
        return ftpClient;
    }

    @Bean
    @ConditionalOnMissingBean
    @Autowired
    public StorageClient ftpFileClient(FtpClient ftpClient) {
        SimpleFtpClient simpleFtpClient = new SimpleFtpClient(properties, ftpClient);
        return simpleFtpClient;
    }
}
